package auctionhaus

import grails.test.mixin.*
import org.junit.*
import grails.plugins.springsecurity.SpringSecurityService


/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(CustomerCreateService)
@Mock([Customer,SpringSecurityService])
class CustomerCreateServiceTests {

    void setUp()
    {
        new Customer(email: "kleczka@gmail.com", password: "1234567", enabled: true).save(flush: true)
    }
    
    void testCreateCustomer()
    {
        def params = [email: "kleczka@abc.com", password: "1234567"]

        Customer c = service.createCustomer(params)

        assert !c.hasErrors()
        assert Customer.findByEmail("kleczka@abc.com")
    }

    void testCreateCustomerBadEmail()
    {
        def params = [email: "kleczka", password: "1234567"]

        Customer c = service.createCustomer(params)

        assert c.hasErrors()
        assert c.errors['email'].code == "email.invalid"
        assert !Customer.findByEmail("kleczka@abc.com")
    }

    void testCreateCustomerBadPasword()
    {
        def params = [email: "kleczka@abc.com", password: "1"]

        Customer c = service.createCustomer(params)

        assert c.hasErrors()
        assert c.errors['password'].code == "minSize.notmet"
        assert !Customer.findByEmail("kleczka@abc.com")

    }

    void testCreateCustomerExistingEmail()
    {
        def params = [email: "kleczka@gmail.com", password: "1234567"]

        Customer c = service.createCustomer(params)

        assert c.hasErrors()
        assert c.errors['email'].code == "unique"
    }
}
